package com.ewhale.feitengguest.app;

import android.app.ActivityManager;
import android.content.Context;

import com.ewhale.feitengguest.ui.user.message.IMChatActivity;
import com.ewhale.feitengguest.ui.user.message.chat.DemoHelper;
import com.ewhale.feitengguest.ui.user.message.chat.Preferences;
import com.ewhale.feitengguest.utils.QiniuUtils;
import com.orhanobut.hawk.Hawk;
import com.simga.library.http.HttpHelper;
import com.simga.library.utils.SdCardUtil;
import com.simga.library.utils.glide.GlideUtil;
import com.youth.xframe.XFrame;

import java.util.Iterator;
import java.util.List;

import androidx.multidex.MultiDexApplication;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jpush.android.api.JPushInterface;

/**
 * PackageName : com.simga.basicsproject.base
 * Author : Simga
 * Date : 2018/06/15
 * Time : 17:16
 */
public class AppApplication extends MultiDexApplication {

    public static Context sApplicationContext;
    private boolean isInit = false;
    public static boolean Jump_invite_list = false;


    @Override
    public void onCreate() {
        super.onCreate();
        sApplicationContext = this;
        //初始化项目文件夹
        SdCardUtil.initFileDir(this);
        //图片框架初始化
        GlideUtil.init(this);
        //初始化hawk
        Hawk.init(this).build();
//        if (Hawk.get(HawkKey.TYPE, 1) == 1) {//普通用户
//            HttpHelper.authentication = Hawk.get(HawkKey.USER_AUTHENTICATION, "");
//        } else {//商家
//            HttpHelper.authentication = Hawk.get(HawkKey.SHOP_AUTHENTICATION, "");
//        }
        //初始化网络框架
        HttpHelper.initHttp(this);
        //初始化七牛
        QiniuUtils.init();
        //初始化极光
        JPushInterface.setDebugMode(true);
        JShareInterface.init(this);
        JPushInterface.init(this);
        //初始化环信
//        initEasemob();
    }

    private void initEasemob() {
        // 获取当前进程 id 并取得进程名
        int pid = android.os.Process.myPid();
        String processAppName = getAppName(pid);
        /**
         * 如果app启用了远程的service，此application:onCreate会被调用2次
         * 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
         * 默认的app会在以包名为默认的process name下运行，如果查到的process name不是app的process name就立即返回
         */
        if (processAppName == null || !processAppName.equalsIgnoreCase(this.getPackageName())) {
            // 则此application的onCreate 是被service 调用的，直接返回
            return;
        }
        if (isInit) {
            return;
        }
        //初始化环信
        Preferences.init(this);
        DemoHelper.getInstance().init(this);
        DemoHelper.getInstance().setChatClz(IMChatActivity.class);
        isInit = true;
    }
    /**
     * 根据Pid获取当前进程的名字，一般就是当前app的包名
     *
     * @param pid 进程的id
     * @return 返回进程的名字
     */
    private String getAppName(int pid) {
        String processName = null;
        ActivityManager activityManager =
                (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        List list = activityManager.getRunningAppProcesses();
        Iterator i = list.iterator();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info =
                    (ActivityManager.RunningAppProcessInfo) (i.next());
            try {
                if (info.pid == pid) {
                    // 根据进程的信息获取当前进程的名字
                    processName = info.processName;
                    // 返回当前进程名
                    return processName;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 没有匹配的项，返回为null
        return null;
    }
}